package com.photoeditor.demo.ui.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.photoeditor.R;
import com.photoeditor.demo.ui.activity.edit.EditorImageActivity;
import com.photoeditor.demo.util.FontUtil;

/**
 * 底部的操作栏
 */
public class BottomInsideBarView extends LinearLayout {

    public static final int TYPE_NAME = 1;

    public static final int TYPE_PROGRESS = 2;

    public static final int TYPE_BUTTON = 3;

    private int mType = TYPE_NAME;

    private ImageView mCancelBt;

    private ImageView mConfirmBt;

    private boolean mConfirmEnable = true;

    private CustomNumSeekBar mSeekBar;

    private TextView mNameTv;

    private View mTextContent;

    private boolean mIsInflated = false;

    private View mButtonContainer;

    private ImageView mFlipHBt;
    private ImageView mFlipVBt;
    private ImageView mRotateRBt;
    private ImageView mLayerUpBt;
    private ImageView mLayerDownBt;

    private EditorImageActivity mActivity;

    public BottomInsideBarView(Context context) {
        this(context, null);
    }

    public BottomInsideBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mCancelBt = (ImageView) this.findViewById(R.id.cancel);
        mConfirmBt = (ImageView) this.findViewById(R.id.confirm);
        mSeekBar = (CustomNumSeekBar) this.findViewById(R.id.seekbar);
        mNameTv = (TextView) this.findViewById(R.id.type_text);
        mTextContent = findViewById(R.id.rl_text);
        mButtonContainer = findViewById(R.id.type_button);
        FontUtil.setCustomFontBold(mNameTv);

        mFlipHBt = (ImageView) findViewById(R.id.flip_horizontal);
        mFlipVBt = (ImageView) findViewById(R.id.flip_vertical);
        mRotateRBt = (ImageView) findViewById(R.id.rotate_right);
        mLayerUpBt = (ImageView) findViewById(R.id.layer_up);
        mLayerDownBt = (ImageView) findViewById(R.id.layer_down);

        Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.ic_tool_rotate_mirror);
        Matrix matrix = new Matrix();
        matrix.setRotate(90);
        mFlipVBt.setImageBitmap(Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true));

        mIsInflated = true;
        initView(getResources().getColor(R.color.windowBackgroundColor),
                getResources().getColor(R.color.accent_color));
    }

    /**
     * 设置当前显示的类型
     *
     * @param type
     */
    public void setType(int type) {
        if (!mIsInflated) return;
        mType = type;
        if (mType == TYPE_NAME) {
            mSeekBar.setVisibility(View.GONE);
            mTextContent.setVisibility(View.VISIBLE);
            mButtonContainer.setVisibility(View.GONE);
        } else if (mType == TYPE_PROGRESS) {
            mSeekBar.setVisibility(View.VISIBLE);
            mTextContent.setVisibility(View.GONE);
            mButtonContainer.setVisibility(View.GONE);
        } else if (mType == TYPE_BUTTON) {
            mButtonContainer.setVisibility(VISIBLE);
            mSeekBar.setVisibility(View.GONE);
            mTextContent.setVisibility(View.GONE);
        }
    }

    public int getType() {
        return mType;
    }

    /**
     * 设置进度
     */
    public void setProgress(int progress) {
        if (mIsInflated) {
            mSeekBar.setProgress(progress);
        }
    }

    public int getProgress() {
        if (mIsInflated) {
            return mSeekBar.getProgress();
        } else {
            return 0;
        }
    }

    public void setActivity(EditorImageActivity activity) {
        mActivity = activity;
    }

    /**
     * 设置名称
     *
     * @param text
     */
    public void setNameText(String text) {
        if (mIsInflated) {
            mNameTv.setText(text);
            mNameTv.setCompoundDrawables(null, null, null, null);
        }
    }

    /**
     * 设置名称
     *
     * @param resId
     */
    public void setNameText(int resId) {
        if (mIsInflated) {
            mNameTv.setText(resId);
            mNameTv.setCompoundDrawables(null, null, null, null);
        }
    }

    public void setNameText(int resId, int drawableId) {
        if (mIsInflated) {
            mNameTv.setText(resId);
//            if(drawableId != View.NO_ID) {
//                Drawable drawable = getResources().getDrawable(drawableId);
//                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//                mNameTv.setCompoundDrawables(null, null, drawable, null);
//                mNameTv.setCompoundDrawablePadding(DeviceUtils.dip2px(ComponentContext.getContext(), 5));
//            } else {
            mNameTv.setCompoundDrawables(null, null, null, null);
//            }
        }
    }

    /**
     * 设置OnClickListener监听
     */
    public void setOnClickListener(OnClickListener listener) {
        if (mIsInflated) {
            mCancelBt.setOnClickListener(listener);
            mConfirmBt.setOnClickListener(listener);
            mFlipHBt.setOnClickListener(listener);
            mFlipVBt.setOnClickListener(listener);
            mRotateRBt.setOnClickListener(listener);
            mLayerUpBt.setOnClickListener(listener);
            mLayerDownBt.setOnClickListener(listener);
            mNameTv.setOnClickListener(listener);
        }
    }

    public void setOnProgressChangeListener(OnSeekBarChangeListener seekBarChangeListener) {
        if (mIsInflated) {
            mSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
        }
    }

    /**
     * 改变颜色
     *
     * @param color
     */
    public void setSeekBarColor(int color) {
        if (mIsInflated) {
            mSeekBar.setColorStyle(color);
        }
    }

    /**
     * 改变颜色
     */
    public void setSeekBarDefaultColor() {
        if (mIsInflated) {
            mSeekBar.setDefaultColorStyle();
        }
    }

    public void showProgressbarWithAnim() {
        mTextContent.setVisibility(View.GONE);
        mButtonContainer.setVisibility(GONE);
        mSeekBar.setVisibility(VISIBLE);
        mSeekBar.startAnimation(mActivity.getBottomInUpToDown());
    }

    public void showTextWithAnim() {
        mSeekBar.setVisibility(View.GONE);
        mButtonContainer.setVisibility(GONE);
        mTextContent.setVisibility(VISIBLE);
        mTextContent.startAnimation(mActivity.getBottomInUpToDown());
    }

    public void setConfirmEnable(boolean flag) {
        if (mIsInflated) {
            if (mType == TYPE_BUTTON) {
                if (flag) {
                    mTextContent.setVisibility(View.GONE);
                    mButtonContainer.setVisibility(View.VISIBLE);
                } else {
                    mTextContent.setVisibility(View.VISIBLE);
                    mButtonContainer.setVisibility(View.GONE);
                }
            }
        }
        if (mIsInflated && mConfirmEnable != flag) {
            if (flag) {
                mConfirmEnable = true;
                mConfirmBt.setImageResource(R.drawable.ic_edit_done_on);
                mConfirmBt.setEnabled(true);
                mFlipHBt.setEnabled(true);

            } else {
                mConfirmEnable = false;
                mConfirmBt.setImageResource(R.drawable.ic_edit_done_off);
                mConfirmBt.setEnabled(false);

            }
        }
    }

    public void setConfirmImageResource(int drawableId) {
        mConfirmBt.setImageResource(drawableId);
    }

    public void initView(int primaryColor, int emphasisColor) {
        mConfirmBt.setImageResource(R.drawable.ic_edit_done_on);
//        mSeekBar.setNumBgTumb(getResources().getDrawable(R.drawable.bg_image_edit_seekbar_num));
//        mSeekBar.setTouchTumb(getResources().getDrawable(R.drawable.bg_image_edit_seekbar_touch));
//        mSeekBar.setProgressTumb(getResources().getDrawable(R.drawable.ic_image_edit_seekbar_progress));
//        mSeekBar.setProgressBgTumb(getResources().getDrawable(R.drawable.bg_image_edit_seekbar_progress));
//        mSeekBar.setTextColor(getResources().getColor(R.color.image_edit_seekbar_text_color));

//        mSeekBar.setDefaultColorStyle(emphasisColor);
    }
}
